package vn.com.vega.music.view.adapter;

import java.util.List;

import vn.com.vega.music.utils.Const;
import vn.com.vega.music.view.adapter.base.AutoLoadingAdapter.AutoLoadingInterface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public class MixAdapter extends BaseAdapter implements AutoLoadingInterface,
		Const {
	private static final int UNKNOW_VIEW_TYPE = -1;

	List<BaseAdapter> listAdapters;

	public MixAdapter(List<BaseAdapter> lisAdapter) {
		this.listAdapters = lisAdapter;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		int count = 0;
		for (BaseAdapter adapter : listAdapters) {
			count += adapter.getCount();
		}
		return count;
	}

	//
	private AdapterAndIndexStart getAdapter(int position) {
		AdapterAndIndexStart result = new AdapterAndIndexStart();
		int leng = 0;
		for (int i = 0; i < listAdapters.size(); i++) {
			BaseAdapter adapter = listAdapters.get(i);
			leng += adapter.getCount();
			if (position < leng) {
				result.indexOfAdapter = i;
				result.indexOfStart = leng - adapter.getCount();
				return result;
			}
		}
		return null;
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		int viewType = getItemViewType(position);
		if (viewType == UNKNOW_VIEW_TYPE) {
			return null;
		}

		AdapterAndIndexStart adapterAndIndexStart = getAdapter(position);
		// BaseAdapter adapter = listAdapters.get(viewType);
		BaseAdapter adapter = listAdapters
				.get(adapterAndIndexStart.indexOfAdapter);
		View view = adapter.getView(position
				- adapterAndIndexStart.indexOfStart, convertView, parent);
		return view;
	}

	@Override
	public int getItemViewType(int position) {
		// TODO Auto-generated method stub
		int leng = 0;
		for (int i = 0; i < listAdapters.size(); i++) {
			leng += listAdapters.get(i).getCount();
			if (position < leng) {
				return i;
			}
		}
		return UNKNOW_VIEW_TYPE;
	}

	@Override
	public int getViewTypeCount() {
		// TODO Auto-generated method stub
		return listAdapters.size();
	}

	static class AdapterAndIndexStart {
		int indexOfAdapter;
		int indexOfStart;
	}

	@Override
	public void notifyListObjectChanged(List<Object> objectsChanged) {
		BaseAdapter lastAdapter = listAdapters.get(listAdapters.size() - 1);
		if (lastAdapter instanceof AutoLoadingInterface) {
			((AutoLoadingInterface) lastAdapter)
					.notifyListObjectChanged(objectsChanged);
		}
	}

	@Override
	public void notifyListObjectChanged(List<Object> objectsChanged, int type) {
		BaseAdapter lastAdapter = listAdapters.get(listAdapters.size() - 1);
		if (lastAdapter instanceof AutoLoadingInterface) {
			((AutoLoadingInterface) lastAdapter).notifyListObjectChanged(
					objectsChanged, type);
		}
	}

}
